package net.huashitong.supervision.task.dao;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import net.huashitong.supervision.task.entity.TaskBackNew;
import net.huashitong.supervision.task.entity.TaskUser;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/8/20
 * Time: 9:20
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Repository
public class TaskBackNewDao extends StringDao<TaskBackNew> {

    public List<TaskBackNew> getByTaskUnitId(String taskUnitId) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null ");

        query.append(" and taskUnitId =:taskUnitId").setParam("taskUnitId",taskUnitId );
        return query.listResult();
    }

    /**
     * 我的反馈
     * @param taskUnitId
     * @param userId
     * @return
     */
    public List<TaskBackNew> getByTaskUnitIdAndSenderId(String taskUnitId, String userId) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null ");

        query.append(" and taskUnitId =:taskUnitId").setParam("taskUnitId",taskUnitId );
        query.append(" and createUser =:createUser").setParam("createUser",userId );
        return query.listResult();
    }

    /**
     * 反馈给我的
     * @param taskUnitId
     * @param userId
     * @return
     */
    public List<TaskBackNew> getByTaskUnitIdAndRecipientId(String taskUnitId, String userId) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null ");

        query.append(" and taskUnitId =:taskUnitId").setParam("taskUnitId",taskUnitId );
        query.append(" and recipientId =:recipientId").setParam("recipientId",userId );
        return query.listResult();
    }

    public List<TaskBackNew> getByTaskUnitIdAndReceiveDeptAndSubId(String taskUnitId, String receiveDept,String subId) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null ");

//        query.append(" and taskUnitId =:taskUnitId").setParam("taskUnitId",taskUnitId );
//        query.append(" and receiveDept =:receiveDept").setParam("receiveDept",receiveDept );
        query.append(" and taskSubId =:taskSubId").setParam("taskSubId",subId );
        query.append(" and recipientId is null");
        return query.listResult();
    }
//根据当前任务的子任务id和接收人id查询反馈信息
    public List<TaskBackNew> getByRecipientIdAndSubId(String recipientId, String taskSubId) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null ");

//        query.append(" and recipientId =:recipientId").setParam("recipientId",recipientId );
        query.append(" and taskSubId =:taskSubId").setParam("taskSubId",taskSubId );
        query.append(" and status =:status").setParam("status","1" );
        return query.listResult();
    }

    public void deleteByUnitIds(List<String> taskUnitIds) {
        StringBuffer hql = (new StringBuffer("delete from TaskBackNew")).append(" obj where obj.taskUnitId in(:taskUnitIds)");
        Query query = this.getSession().createQuery(hql.toString());
        query.setParameterList("taskUnitIds", taskUnitIds);
        query.executeUpdate();
    }

    /**
     * 某个日期后的反馈集合
     * @param pastDate
     * @return
     */
    public List<TaskBackNew> queryBeforeDate(String pastDate) {
        GenericQuery obj = createQuery("obj");
        obj.append(" and del is null");
        obj.append(" and createDate >:createDate").setParam("createDate", pastDate);
        return obj.listResult();
    }
}
